from rest_framework.response import Response
from rest_framework.views import APIView

from apps.classify.models import WorksCategory
from apps.classify.serializers import WorksCategorySerializer


class ClassifyView(APIView):
    '''分类页'''

    def get(self, request, *args, **kwargs):
        works = WorksCategory.objects.filter(parent=None)  # 一级分类
        serializer = WorksCategorySerializer(works, many=True)

        ificationTitle = serializer.data

        list01 = []
        # [{
        #     'title': '',
        #     'content': [],
        # }]
        for i in ificationTitle:
            b = {}
            b['id'] = i['id']
            b['title'] = i['name']
            works_b = WorksCategory.objects.filter(parent=i['id'])
            d = WorksCategorySerializer(works_b, many=True)
            e = d.data
            # print(len(e))
            b['content'] = []
            if len(e) <= 6:
                b['content'].append(e)
            else:
                f = e[0:6]
                b['content'].append(f)
            # print(b['content'])
            list01.append(b)

        # 2. 返回应答
        response_data = {
            'ificationTitle': ificationTitle,
            'ificationContent': list01,
        }

        return Response(response_data)

# ificationTitle: [
#     {title: "有声书"},
#     {title: "畅销书"},
#     {title: "儿童"},
#     {title: "相声评书"},
#     {title: "情感生活"},
#     {title: "人文"},
#     {title: "历史"},
#     {title: "国学书院"},
#     {title: "音乐"},
#     {title: "英语"},
#     {title: "教育培训"},
#     {title: "健康养生"},
#     {title: "广播剧"},
#     {title: "戏曲"}
# ],
# ificationContent: [
#     {
#         title: "有声书",
#         content: [
#             {text: "言情"},
#             {text: "悬疑"},
#             {text: "都市"},
#             {text: "幻想"},
#             {text: "武侠"},
#             {text: "历史"}
#         ]
#     },
#     {
#         title: "畅销书",
#         content: [
#             {text: "社科"},
#             {text: "经管"},
#             {text: "文学"},
#             {text: "励志"},
#             {text: "名著"},
#             {text: "生活"}
#         ]
#     },
#     {
#         title: "儿童",
#         content: [
#             {text: "故事"},
#             {text: "儿歌"},
#             {text: "动画"},
#             {text: "科普"},
#             {text: "名著"},
#             {text: "国学"}
#         ]
#     },
#     {
#         title: "相声评书",
#         content: [
#             {text: "单田芳"},
#             {text: "郭德纲"},
#             {text: "热门相声"},
#             {text: "单口相声"},
#             {text: "名家评书"},
#             {text: "新锐笑将"}
#         ]
#     },
#     {
#         title: "情感生活",
#         content: [
#             {text: "恋爱技巧"},
#             {text: "睡前夜话"},
#             {text: "情绪压力"},
#             {text: "婚姻家庭"},
#             {text: "个人成长"},
#             {text: "心理健康"}
#         ]
#     },
#     {
#         title: "人文",
#         content: [
#             {text: "言情"},
#             {text: "悬疑"},
#             {text: "都市"},
#             {text: "幻想"},
#             {text: "武侠"},
#             {text: "历史"}
#         ]
#     },
#     {
#         title: "历史",
#         content: [
#             {text: "言情"},
#             {text: "悬疑"},
#             {text: "都市"},
#             {text: "幻想"},
#             {text: "武侠"},
#             {text: "历史"}
#         ]
#     },
#     {
#         title: "国学书院",
#         content: [
#             {text: "言情"},
#             {text: "悬疑"},
#             {text: "都市"},
#             {text: "幻想"},
#             {text: "武侠"},
#             {text: "历史"}
#         ]
#     },
#     {
#         title: "音乐",
#         content: [
#             {text: "言情"},
#             {text: "悬疑"},
#             {text: "都市"},
#             {text: "幻想"},
#             {text: "武侠"},
#             {text: "历史"}
#         ]
#     },
#     {
#         title: "英语",
#         content: [
#             {text: "言情"},
#             {text: "悬疑"},
#             {text: "都市"},
#             {text: "幻想"},
#             {text: "武侠"},
#             {text: "历史"}
#         ]
#     },
#     {
#         title: "教育培训",
#         content: [
#             {text: "言情"},
#             {text: "悬疑"},
#             {text: "都市"},
#             {text: "幻想"},
#             {text: "武侠"},
#             {text: "历史"}
#         ]
#     },
#     {
#         title: "健康养生",
#         content: [
#             {text: "言情"},
#             {text: "悬疑"},
#             {text: "都市"},
#             {text: "幻想"},
#             {text: "武侠"},
#             {text: "历史"}
#         ]
#     },
#     {
#         title: "广播剧",
#         content: [
#             {text: "言情"},
#             {text: "悬疑"},
#             {text: "都市"},
#             {text: "幻想"},
#             {text: "武侠"},
#             {text: "历史"}
#         ]
#     },
#     {
#         title: "戏曲",
#         content: [
#             {text: "言情"},
#             {text: "悬疑"},
#             {text: "都市"},
#             {text: "幻想"},
#             {text: "武侠"},
#             {text: "历史"}
#         ]
#     },
# ]


class ClassifyListView(APIView):
    """分类列表页"""
    def get(self, request, *args, **kwargs):
        id = self.kwargs['pk']
        works = WorksCategory.objects.filter(parent=id)
        serializer = WorksCategorySerializer(works, many=True)
        navTitle = serializer.data
        work = WorksCategory.objects.get(id=id)
        title = work.name
        if not navTitle:
            parent_id = work.parent.id
            title = work.parent.name
            works = WorksCategory.objects.filter(parent=parent_id)
            serializer = WorksCategorySerializer(works, many=True)
            navTitle = serializer.data
        # print(title)
        # print(navTitle)
        # 2. 返回应答
        response_data = {
            'navTitle': navTitle,
            'title': title
        }

        return Response(response_data)
